import axios from "axios";
let baseURL = 'http://localhost:3000/'
const service = axios.create({
    baseURL: baseURL,
    timeout: 10000
})

//请求拦截器
service.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token')
        if (token) {
            config.headers['x-acess-token'] = token
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

//响应拦截器
service.interceptors.response.use(
    response => {
        const res = response.data
        if (res.code !== 200) {
            return Promise.reject(new Error(res.message || 'Error'))
        } else {
            if (res.code == 200) {
                return res.result
            } else {
                alert(res.sucess)
            }
        }

    },
    error => {
        return Promise.reject(error)
    }
)
export default service